<?php

class Core_Component_Menu_Controller_Backend_Menu {
    
    public function __construct() {
        $GLOBALS[RC]->getHelper()->setLocation('{menu.menu_manage}');
    }   
    
    public function mappingAction(){
        return array(
            'create'=>'edit'
        );
    }
    
    public function indexAction($pageindex = 1, $pagesize = 20){
        $GLOBALS[RC]->getHelper()->setLocation('{menu.menu_index}');
        $q = App::getModel('menu/menu')->getQuery();
        $list = $q->pagination($pageindex, $pagesize)->fetchCollection();
        $count = $q->fetchCount();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list' => $list, 'record_count' => $count));
    } 
    
    public function chooseAction(){
        $GLOBALS[RC]->getHelper()->setLocation('{menu.create_menu}', '{menu.choose_menu}');
        $list = App::getModel('menu/category')
                ->getQuery()
                ->fetch();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list'=>$list->toArray()));
    }    
    
    public function editAction($categoryid = NULL, $id = NULL) {
        if(!is_null($id)){
            $model = App::loadModel('menu/menu', $id);
            $model->addData($model->getArgument());
            $category = App::loadModel('menu/category', $model->getCategoryId());
            $menus = App::getModel('menu/menu')
                        ->getQuery()
                        ->condition('v.website_id', App::getRunningWebsite()->getId())
                        ->condition('v.version_id', $id, 'neq')
                        ->fetch()
                        ->toArray();            
            $GLOBALS[RC]->getHelper()->setLocation($GLOBALS[RC]->getLanguage()->__($category->getName()), $model->getName());
        }else{
            $model = App::getModel('menu/menu')->setCategoryId($categoryid);
            $category = App::loadModel('menu/category', $model->getCategoryId());
            $menus = App::getModel('menu/menu')
                        ->getQuery()
                        ->condition('v.website_id', App::getRunningWebsite()->getId())
                        ->fetch()
                        ->toArray();
            $GLOBALS[RC]->getHelper()->setLocation('{menu.create_menu}', $GLOBALS[RC]->getLanguage()->__($category->getName()));
        }
        $com = App::loadModel('system/component', $category->getComponentId());
        $roles = App::getModel('account/role')->getQuery()->fetch()->toArray();        
        $filename = DP_DESIGN.'backend'.DS.'default'.DS.'component'.
                    DS.strtolower($com->getCode()).
                    DS.'menuform'.
                    DS.$category->getCode().'.phtml';
        if(file_exists($filename)){
            $result = $GLOBALS[RC]->getHelper()->modelFormResult(
                $model,
                array(
                    'role_codes'=>array('data'=>$roles,'labelKey'=>'name', 'valueKey'=>'code'),
                    'parent_id'=>array('data'=>$menus)
                ), $filename);
        }
        return $result;
    }

    public function saveAction(Core_Component_Menu_Model_Menu $model) {
        $model->merge()->validate()->save();
        return $GLOBALS[RC]->getHelper()->redirectResult('*/*/*/index');
    }    
    
    public function sortAction($parentid){
        $GLOBALS[RC]->getHelper()->setLocation('{menu.menu_sort}');
        $list = App::getModel('menu/menu')
                    ->getQuery()
                    ->condition('m.parent_id', $parentid)
                    ->condition('v.website_id', App::getRunningWebsite()->getId())
                    ->orderby('m.sort', 'asc')
                    ->fetch();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list'=>$list->toArray()));
    }
    
    public function sortPostAction($ids){
        foreach($ids as $index=>$id){
            App::getModel('menu/menu')
                    ->loadBy(array('id'=>$id))
                    ->setSort($index + 1)
                    ->save();
        }
        $GLOBALS[RC]->getMessager()->addInformation('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');         
    }
    
    public function enableAction($ids){
        App::getModel('menu/menu')
                ->setStatus(CONSTANT_STATUS_ENABLED)
                ->getVersionUpdate()
                ->condition('version_id', $ids, 'in')
                ->execute();
        $GLOBALS[RC]->getMessager()->addInformation('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');        
    }
    
    public function disableAction($ids){
        $update = App::getModel('menu/menu')
                    ->setStatus(CONSTANT_STATUS_DISABLED)
                    ->getVersionUpdate();
        $update->condition('version_id', $ids, 'in')->execute();    
        $GLOBALS[RC]->getMessager()->addInformation('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');      
    }    
    
    public function deleteAction($ids){
        $menus = App::getModel('menu/menu')
                    ->getQuery()
                    ->condition('v.version_id', $ids, 'in')
                    ->fetchCollection();
        foreach($menus as $menu){
            $menu->delete();
        }
        $GLOBALS[RC]->getMessager()->addInformation('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');    
    }
}